package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmUserDto;
import com.heima.model.wemedia.pojo.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 自媒体用户信息
 */
@Service
@Transactional
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper,WmUser> implements WmUserService {


    /**
     * 自媒体用户查询
     * @param name
     * @return
     */
    @Override
    public WmUser findByName(String name) {
        //1.判断参数是否为空
        if(StringUtils.isBlank(name)){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.查询自媒体用户
        WmUser wmUser = getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if(wmUser==null){
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        return wmUser;
    }

    /**
     * 自媒体用户保存
     * @param wmUser
     * @return
     */

    @Override
    public ResponseResult insert(WmUser wmUser) {
        //1.判断参数是否为空
        if(wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存自媒体用户
        save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 自媒体用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto dto) {
        //1,校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2,校验用户名，密码是否为空
        if(StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }
        //2,查询数据库是否存在该用户
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName,dto.getName());
        WmUser user = getOne(lambdaQueryWrapper);
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }

        //3,如果用户存在，校验密码是否正确，如果密码错误，响应密码错误，正确返回token
        String testPwd = dto.getPassword();
        String salt=user.getSalt();
        String loginPwd=testPwd+salt;

        //3.1校验登录密码加密+盐
        String pwdEncrypt = DigestUtils.md5DigestAsHex(loginPwd.getBytes());

        //3.1校验登录密码成功
        if(pwdEncrypt.equals(user.getPassword())){
           //3.2根据用户id，为登录成功的id生成token
            String token = AppJwtUtil.getToken(user.getId().longValue());
            Map useMap=new HashMap();
            useMap.put("token",token);
            user.setPassword("");
            user.setSalt("");
            useMap.put("user",user);
            return ResponseResult.okResult(useMap);

        }else {
            //3.1如果密码错误，响应密码错误
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
        }
    }
}
